﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Input;
using Squog_1._1.Managers;

namespace Squog_1._1.Menu
{
    class SettingsWindow : MainMenuWindow
    {
        //Needed to draw the safe zone border
        private Texture2D Px;

        //Button Layout for the button in this window
        ButtonLayout Layout;

        WindowButton GamePadButton;

        private bool AdjustingSafeZone;

        public SettingsWindow()
            : base()
        {
            AdjustingSafeZone = false;
        }

        public override void LoadContent(ContentManager Content)
        {
            //Load the Pixel Texture for safe zone selection
            Px = Content.Load<Texture2D>(@"Menu\px");
            base.LoadContent(Content);

            //Create the layout for the buttons
            Layout = new ButtonLayout(new Vector2(0,0));

            //Initialize the buttons
            Layout.AddButtonToEnd("Adjust Safe Zone", MenuFont, true);
            Layout.AddButtonToEnd(GamePadButton = new WindowButton("Enable Gamepad", MenuFont, GamePad.GetState(PlayerIndex.One).IsConnected));
            Layout.AddButtonToEnd("Back", MenuFont, true);
        }

        private void OnSelectHandeler(object o, SelectedEventArgs e)
        {
            switch (e.SelectionNumber)
            {
                case 0:
                    AdjustingSafeZone = true;
                    break;
                case 1:
                    InputManager.UseGamePad = !InputManager.UseGamePad;
                    if (InputManager.UseGamePad)
                        GamePadButton.Text = "Disable Gamepad";
                    else
                        GamePadButton.Text = "Enable Gamepad";
                    break;
                default:
                    WindowManager.ChangeWindows(new MainWindow());
                    break;
            }
        }

        public override void Update(GameTime gameTime)
        {
            Layout.Update();

            if (!GamePad.GetState(PlayerIndex.One).IsConnected && InputManager.UseGamePad)
            {
                InputManager.UseGamePad = false;
                GamePadButton.Text = "Enable Gamepad";
            }

            GamePadButton.Selectable = GamePad.GetState(PlayerIndex.One).IsConnected;

            if (AdjustingSafeZone)
            {
                if(InputManager.OnPress(Keys.Enter, 0) ||
                   InputManager.OnPress(Buttons.A, 0) ||
                   InputManager.OnPress(Buttons.Start, 0))
                    AdjustingSafeZone = false;
            }

            base.Update(gameTime);
        }

        public override void Draw(GameTime gameTime)
        {
            base.Draw(gameTime);

            Layout.DrawButtons(WindowsSpriteBatch);

            if (AdjustingSafeZone)
            {
                WindowsSpriteBatch.Draw(Px, new Rectangle(Settings.SafeZone.X, Settings.SafeZone.Y, Settings.SafeZone.Width, 2), Color.White);
                WindowsSpriteBatch.Draw(Px, new Rectangle(Settings.SafeZone.X, Settings.SafeZone.Y + Settings.SafeZone.Height, Settings.SafeZone.Width, 2), Color.White);
                WindowsSpriteBatch.Draw(Px, new Rectangle(Settings.SafeZone.X, Settings.SafeZone.Y, 2, Settings.SafeZone.Height), Color.White);
                WindowsSpriteBatch.Draw(Px, new Rectangle(Settings.SafeZone.X + Settings.SafeZone.Width, Settings.SafeZone.Y, 2, Settings.SafeZone.Height), Color.White);
            }
        }
    }
}
